package com.lizhenhua.fmmall.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lizhenhua.fmmall.entity.ShoppingCart;
import com.lizhenhua.fmmall.entity.Users;
import com.lizhenhua.fmmall.interceptor.CheckTokenInterceptor;
import com.lizhenhua.fmmall.service.serviceImpl.ShopCarServiceImpl;
import com.lizhenhua.fmmall.utils.Base64Utils;
import com.lizhenhua.fmmall.vo.ResStatus;
import com.lizhenhua.fmmall.vo.ResultVo;
import io.jsonwebtoken.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/shopcart")
@CrossOrigin
@RestController
@Api(value = "提供购物车业务相关的接口", tags = "购物车管理")
public class ShopcartController {
    @Autowired
    private ShopCarServiceImpl carService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @ApiOperation("用户购物车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "uId", value = "用户id", required = true, defaultValue = "15")
    })
    @GetMapping("/list/{uId}")
    public ResultVo listCarsByUid(@PathVariable("uId") String uId, @RequestHeader("token") String token) {

        ResultVo resultVo = carService.selectShopListById(uId);
        //1.获取token
        //2.检验token
        if (resultVo == null) {
            return new ResultVo(ResStatus.OK, "添加购物车失败", null);
        } else {
            return new ResultVo(ResStatus.OK, "success", resultVo);
        }
    }
//    @ApiImplicitParams({
//        dataType数据类型     required = true 必须填写  defaultValue默认值
//            @ApiImplicitParam(dataType = "string", name = "token", value = "授权令牌", required = true),
//    })


    @ApiOperation(value = "购物车 商品详情接口")
    @ApiImplicitParams({
            // dataType数据类型     required = true 必须填写  defaultValue默认值
            @ApiImplicitParam(dataType = "ShoppingCart", name = "cart", value = "ShoppingCart 对象", required = true),
    })
    @PostMapping("/add") //get请求不能传data
    public ResultVo addShopCarts(@RequestBody ShoppingCart cart, @RequestHeader("token") String token) {

        ResultVo resultVo = carService.addShopCart(cart);
        String s = stringRedisTemplate.boundValueOps(token).get();
        try {
            Users users = objectMapper.readValue(s, Users.class);
            System.out.println(users);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultVo;

    }

    @ApiOperation(value = "购物车-商品修改接口")
    @ApiImplicitParams({
            // dataType数据类型     required = true 必须填写  defaultValue默认值
            @ApiImplicitParam(dataType = "String", name = "cartNum", value = "商品数量", required = true),
            @ApiImplicitParam(dataType = "Integer", name = "cartId", value = "商品id", required = true),
    })
    @PutMapping("/update/{cartNum}/{cartId}")  //restful风格
    public ResultVo updateCartNumByCartId(@PathVariable("cartNum") String cartNum, @PathVariable("cartId") Integer cartId, @RequestHeader("token") String token) {
        ResultVo resultVo = carService.updateCartNumByCartId(cartNum, cartId);
        return resultVo;
    }


    @ApiOperation("商品-用户结算的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "java.lang.List", name = "cids", value = "用户选中的商品id", required = true)
    })
    @GetMapping("/listBycids")  // get 请求不能传请求体
    public ResultVo listCarsByCid(String cids, @RequestHeader("token") String token) {

        ResultVo resultVo = carService.listShopListByCids(cids);
        //1.获取token
        //2.检验token
        return resultVo;


    }
}
